package com.lxw.oa.controller.common;

import com.lxw.oa.controller.io.BaseTo;
import com.lxw.oa.entity.User;
import com.lxw.oa.service.UserService;
import com.lxw.oa.util.LogAopIn;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.misc.BASE64Decoder;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.List;

@Controller
@RequestMapping("/personal")
public class PersonalController {

    @Autowired
    private UserService userService;



    @LogAopIn(moduleName="首页-个人中心",option="页面初始化")
    @GetMapping("/getPersonalCenter")
    public String Log_getPersonalCenter(Model model){
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        String  userName = user.getName();
        List<String> imgUrl=userService.userImgsGet();
        model.addAttribute("userName",userName);
        model.addAttribute("imgUrl",imgUrl);
        return "pages/personal/personalCenter";
    }
    @PostMapping("/savePhoto")
    @ResponseBody
    public BaseTo savePhoto(String image, HttpServletRequest request,String userName){
        userService.userImgDel(userName);
        userService.userImgAdd(image,userName);
        return new BaseTo("ok");
    }

    @LogAopIn(moduleName="资源管理-头像管理",option="上传头像")
    @PostMapping("/savePhotos")
    @ResponseBody
    public BaseTo Log_savePhotos(String image){
        userService.userImgsAdd(image);
        return new BaseTo("ok");
    }

    @PostMapping("/getPhoto")
    @ResponseBody
    public BaseTo getPhoto(String userName){
        List<String> imgUrl=userService.userImgGet(userName);
        return new BaseTo(imgUrl);
    }


    @PostMapping("/getPhotos")
    @ResponseBody
    public BaseTo getPhotos(){
        List<String> imgUrl=userService.userImgsGet();
        return new BaseTo(imgUrl);
    }



}
